package com.example.pocket_demo.controller;

import com.example.pocket_demo.entity.MsgData;
import com.example.pocket_demo.entity.Record;
import com.example.pocket_demo.entity.User;
import com.example.pocket_demo.entity.vo.PocketVo;
import com.example.pocket_demo.entity.vo.RecordVO;
import com.example.pocket_demo.service.PocketService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;

@RestController
@RequestMapping("/pocket")
public class PocketController {

    @Autowired
    PocketService pocketService;

    //查询余额接口
    @GetMapping("/getBalance")
    public MsgData<BigDecimal> getBalance(User user) throws Exception {
        try {
            BigDecimal balance = pocketService.getBalance(user.getUserid());
            if(balance==null){
                return new MsgData<>(1,null,"invalid param");
            }else{
                return new MsgData<>(0,balance,"Success");
            }
        }catch (Exception e){
            return new MsgData<>(1,null,e.getMessage());
        }
    }


    //消费接口
    @PostMapping("/consume")
    public MsgData<BigDecimal> Consume(@RequestBody PocketVo pocketVo) {
        try {
            BigDecimal restAmount = pocketService.consume(pocketVo.getUserid(),pocketVo.getAmount());
            return new MsgData<>(0,restAmount,"Success");
        }catch (Exception e){
            return new MsgData<>(1,null,e.getMessage());
        }
    }

    //退款接口
    @PostMapping("/refund")
    public MsgData<BigDecimal> refund(@RequestBody PocketVo pocketVo){
        try {
            BigDecimal restAmount = pocketService.refund(pocketVo.getUserid(),pocketVo.getAmount());
            return new MsgData<>(0,restAmount,"Success");
        }catch (Exception e){
            return new MsgData<>(1,null,e.getMessage());
        }
    }

    //明细接口
    @PostMapping("/listIncomeRefund")
    public MsgData<List<RecordVO>> listIncomeRefund(@RequestBody RecordVO recordVO){
        try {
            List<RecordVO> result = pocketService.listIncomeRefund(recordVO.getPocketid(),recordVO.getPage(),recordVO.getPagesize());
            return new MsgData<>(0,result,"Success");
        }catch (Exception e){
            return new MsgData<>(1,null,e.getMessage());
        }
    }
}
